/*******************************************************************************
 * Copyright (c) 2010, 2030 www.itlaobing.cn
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 *******************************************************************************/
package cn.itlaobing.dao;

import cn.itlaobing.model.GoodsModel;
import cn.itlaobing.model.GoodsStatus;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * ClassName: GoodsDao
 * Function: TODO 商品的数据访问层
 * date: 2017年11月6日08:33:43
 * 
 * @author xuaner 
 * @version  
 * @since JDK 1.8 
 */
public class GoodsDao extends BaseDao {
    public List<GoodsModel> queryAll(int userId)throws SQLException,ClassNotFoundException{
        String sql="select *from goods where goods_uid=? order by sell_time DESC";
        QueryRunner qr=new QueryRunner(getDataSource());
        BeanListHandler<GoodsModel> goodsModelBeanListHandler=new BeanListHandler<GoodsModel>(GoodsModel.class);
        return qr.query(sql,goodsModelBeanListHandler,userId);
    }
    /**
     * 支付
     *
     */
    public GoodsModel payment(String goods_id)throws SQLException,ClassNotFoundException{
        //验证订单状态是否为未支付,根据订单编号查询
        QueryRunner qr=new QueryRunner(this.getDataSource());
        String sql="SELECT *FROM goods WHERE goods_id=?";
        BeanHandler<GoodsModel> goodsModelBeanHandler=new BeanHandler<GoodsModel>(GoodsModel.class);
        GoodsModel gm=qr.query(sql,goodsModelBeanHandler,goods_id);
        if(gm==null){
            return null;
        }
        if(gm.getStatus()== GoodsStatus.NO_PAYMENT){
            sql="UPDATE goods SET goods_status=? WHERE goods_no=?";
            qr.update(sql,GoodsStatus.ALLOCATE.name(),gm.getGoods_no());
            gm.setStatus(GoodsStatus.NO_PAYMENT);
            return gm;
        }
        return null;
    }
}